﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace SundUnderviser.DBLayer
{
    public class DBConnection
    {
      //  private  static readonly string connectionString = @"Server=KBHA-THINK\SQLEXPRESS2012;Database=Sundprojekt;User Id=sa;Password=MasterKey;MultipleActiveResultSets=True";
        private static Boolean isCreated = false;

        public static readonly string connectionString = @"Server=balder.ucn.dk;Database=SundTek;User Id=SundTek;Password=Koden er sikker!;";
        public static readonly SqlConnection dbconn = new SqlConnection(connectionString);

        private static SqlCommand dbCmd;
       


        private static void Open()
        {

            Console.WriteLine("connect to: " + connectionString);


            if (dbconn.State.ToString().CompareTo("Open") != 0)
                try
                {
                    dbconn.Open();
                }
                catch (Exception e)
                {
                    Console.WriteLine("problem with open");
                    Console.WriteLine(e);

                }
        }

        public static void Close()
        {
            dbconn.Close();
        }
        public static SqlConnection getConnection()
        {
            Console.WriteLine("connectionstring " + connectionString);

            if (dbconn.State.ToString().CompareTo("Open") != 0)
                Open();
            return dbconn;
        }

        public static SqlCommand GetDbCommand(string sql)
        {
            if (dbconn.State.ToString().CompareTo("Open") != 0)
                Open();
            if (dbCmd == null)
            {
                dbCmd = new SqlCommand(sql, dbconn);
            }
            dbCmd.CommandText = sql;
            return dbCmd;
        }
        public static SqlCommand GetDbCommand()
        {
            if (dbconn.State.ToString().CompareTo("Open") != 0)
                Open();
            if (dbCmd == null)
            {
                dbCmd = new SqlCommand();
            }
           
            return dbCmd;
        }
        public static Boolean isCreatedM()
        {
            if (isCreated)
            {
                return true;
            }
            else{
                return false;
        }
        }
        public static void setIsCreated()
        {
            if (!isCreated)
            {
                isCreated = true;
            }
        }



    }
}

